Chapter 14

Exercise 3

library(pacman)
p_load(ggplot2, macleish, lubridate, zoo, plotly,shiny)

whately_2015$when <- as.Date(whately_2015$when)

yq <- as.yearqtr(as.yearmon(whately_2015$when, "%Y/%m/%d") + 1/12)
whately_2015$Season <- factor(format(yq, "%q"), levels = 1:4,
                labels = c("Winter Solstice", "Vernal Equinox", "Summer Solstice", "Autumnal Equinox"))
head(whately_2015)
## # A tibble: 6 × 9
##   when       temperature wind_s…¹ wind_…² rel_h…³ press…⁴ solar…⁵ rainf…⁶ Season
##   <date>           <dbl>    <dbl>   <dbl>   <dbl>   <int>   <dbl>   <dbl> <fct> 
## 1 2015-01-01       -9.32     1.40    225.    54.6     985       0       0 Winte…
## 2 2015-01-01       -9.46     1.51    248.    55.4     985       0       0 Winte…
## 3 2015-01-01       -9.44     1.62    258.    56.2     985       0       0 Winte…
## 4 2015-01-01       -9.3      1.14    244.    56.4     985       0       0 Winte…
## 5 2015-01-01       -9.32     1.22    238.    56.9     984       0       0 Winte…
## 6 2015-01-01       -9.34     1.09    242.    57.2     984       0       0 Winte…
## # … with abbreviated variable names ¹​wind_speed, ²​wind_dir, ³​rel_humidity,
## #   ⁴​pressure, ⁵​solar_radiation, ⁶​rainfall
whately_2015_plot <- ggplot(data = whately_2015, aes(x = when, y = temperature)) +
  geom_point(aes(color = Season), size = 2)
ggplotly(whately_2015_plot)

Exercise 4

p_load(tidyverse, shiny, shinybusy,mdsr, DT)

mergedViolations <- Violations %>%
  left_join(Cuisines)
## Joining, by = "cuisine_code"
mergedViolations %>%
  select(dba,boro,cuisine_description) %>% 
  group_by(dba) %>%
  count(cuisine_description) %>%
  filter(cuisine_description == "Pizza")
## # A tibble: 842 × 3
## # Groups:   dba [842]
##    dba                      cuisine_description     n
##    <chr>                    <fct>               <int>
##  1 $1 PIZZA $2 BEER         Pizza                  46
##  2 10 DEVOE                 Pizza                   3
##  3 10TH AVENUE PIZZA & CAFE Pizza                  32
##  4 18 EAST GUNHILL PIZZA    Pizza                  14
##  5 187TH ST PIZZA           Pizza                   1
##  6 2 BRO'S PIZZA            Pizza                   3
##  7 2 BROS                   Pizza                  31
##  8 2 BROS PIZZA             Pizza                 189
##  9 2 HARLEM BROS PIZZA      Pizza                   4
## 10 44TH STREET PIZZA        Pizza                  19
## # … with 832 more rows
mergedViolations %>%
  select(dba,boro,cuisine_description) %>% 
 filter(boro == "BROOKLYN") %>%
  group_by(dba) %>%
  count(cuisine_description) %>%
  filter(cuisine_description == "Caribbean")
## # A tibble: 281 × 3
## # Groups:   dba [281]
##    dba                              cuisine_description     n
##    <chr>                            <fct>               <int>
##  1 739 FRANKLIN BAR & LOUNGE        Caribbean              19
##  2 A & C GUYANA BAKERY & RESTAURANT Caribbean              13
##  3 A & P ROTI & PASTRY SHOP         Caribbean               7
##  4 ABEGALE'S                        Caribbean              13
##  5 ALI'S ROTI SHOP                  Caribbean              38
##  6 ALI'S TRINIDAD ROTI SHOP         Caribbean              19
##  7 ALICE'S PALACE                   Caribbean              12
##  8 ALL NATIONS                      Caribbean               9
##  9 AMBIANCE EXPRESS                 Caribbean              13
## 10 ANGEL FLAKES PATTIES             Caribbean              43
## # … with 271 more rows
ui <- fluidPage(
  titlePanel("Number of Restaurants in a Cuisine"),
  fluidRow(
    # some things take time: this lets users know
    add_busy_spinner(spin = "fading-circle"),
    column(
      4,
      selectInput(inputId = "boro",
                  label = "Borough:",
                  choices = c(
                    "ALL",
                    unique(as.character(mergedViolations$boro))
                  )
      )
    ),
    # display dynamic list of cuisines
    column(4, uiOutput("cuisinecontrols"))
  ),
  # Create a new row for the table.
  fluidRow(
    DT::dataTableOutput("table")
  )
)

server <- function(input, output) {
  datasetboro <- reactive({ # Filter data based on selections
    req(input$boro)
    data <- mergedViolations %>%
      select(dba,boro,cuisine_description) %>% 
    group_by(dba) %>%
    count(cuisine_description)

})
  
  datasetcuisine <- reactive({  # dynamic list of cuisines
    req(input$cuisine)   # wait until list is available
    data <- datasetboro() %>%
      unique()
    if (input$cuisine != "ALL") {
      data <- data %>%
        filter(cuisine_description == input$cuisine)
    }
    data
  })
  
  output$table <- DT::renderDataTable(DT::datatable(datasetcuisine()))
  
  output$cuisinecontrols <- renderUI({
    availablelevels <-
      unique(sort(as.character(datasetboro()$cuisine_description)))
    selectInput(
      inputId = "cuisine",
      label = "Cuisine:",
      choices = c("ALL", availablelevels)
    )
  })
}

shinyApp(ui = ui, server = server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents

Exercise 6

p_load(ggExtra, nasaweather)

p <- ggplot(storms, aes(x = wind, y = pressure)) +
  geom_point() +
  theme_classic() +
  stat_smooth(method = "loess", formula = y ~ x, size = 2)
ggExtra::ggMarginal(p, type = "histogram", binwidth = 3)

This scatterplot shows a negative linear association between wind and pressure in Tropical cyclone tracks through the Atlantic Ocean, Caribbean Sea and Gulf of Mexico from 1995 to 2005, as the pressure increases, the wind decreases. The histograms also give us an insight between the relationship between the two variables. The pressure is higher in millibars when the wind is less than 40 knots.

Exercise 7

library(shiny)

p_load(tidyverse, shiny, shinybusy,mdsr, DT, palmerpenguins)

penguins<- palmerpenguins::penguins




ui <- fluidPage(
  titlePanel("Palmer Penguins"),
  fluidRow(
    # some things take time: this lets users know
    add_busy_spinner(spin = "fading-circle"),
    column(
      4,
      selectInput(inputId = "species",
                  label = "Select Species:",
                  choices = c(
                    "ALL",
                    unique(as.character(penguins$species))
                  )
      )
    ),
    # display dynamic list of cuisines
    column(4, uiOutput("speciescontrols"))
  ),
  # Create a new row for the table.
  fluidRow(
    DT::dataTableOutput("table")
  )
)

server <- function(input, output) {
  datasetspecies <- reactive({ # Filter data based on selections
    req(input$species)
    data <- penguins %>%
      select(species,sex,island,bill_length_mm, bill_depth_mm,flipper_length_mm,body_mass_g, year ) %>%
      group_by(species)
      
    
  })
  
  datasetsex <- reactive({  # dynamic list of cuisines
    req(input$sex)   # wait until list is available
    data <- datasetspecies() %>%
      unique()
    if (input$sex != "ALL") {
      data <- data %>%
        filter(sex == input$sex)
    }
    data
  })
  
  output$table <- DT::renderDataTable(DT::datatable(datasetsex()))
  
  output$speciescontrols <- renderUI({
    availablelevels <-
      unique(sort(as.character(datasetspecies()$sex)))
    selectInput(
      inputId = "sex",
      label = "Select Gender:",
      choices = c("ALL", availablelevels)
    )
  })
}


shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents

Exercise 8

p_load(macleish, dplyr)

whately<- macleish::whately_2015
orchard<- macleish::orchard_2015

ui <- fluidPage(
  titlePanel("Weather data"),
  fluidRow(
    # some things take time: this lets users know
    add_busy_spinner(spin = "fading-circle"),
    column(
      4,
      selectInput(inputId = "data",
                  label = "Select Data:",
                  choices = c(
                    "orchard", "whately"
                  
                  )
      )
    ),
    # display dynamic list of cuisines
    column(4, uiOutput("datacontrols"))
  ),
  # Create a new row for the table.
  fluidRow(
    DT::dataTableOutput("table")
  )
)

server <- function(input, output) {
  datasetweather <- reactive({ # Filter data based on selections
    req(input$data)
    data <- select(orchard, whately)
    
    
  })
  
  datasetwhen <- reactive({  # dynamic list of cuisines
    req(input$when)   # wait until list is available
    data <- datasetweather() %>%
      unique()
    if (input$when != "ALL") {
      data <- data %>%
        filter(when == input$when)
    }
    data
  })
  
  output$table <- DT::renderDataTable(DT::datatable(datasetwhen()))
  
  output$datacontrols <- renderUI({
    availablelevels <-
      unique(sort(as.character(datasetweather()$when)))
    selectInput(
      inputId = "when",
      label = "Select Date:",
      choices = c("ALL", availablelevels)
    )
  })
}

shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents